<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">IP地址</label>
        <el-input
          v-model="query.ip"
          clearable
          placeholder="IP地址"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              allow-create
              :remote-method="getLabs"
              :loading="searchLoading"
              placeholder="请选择位置"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="IP地址" prop="ip">
            <el-input v-model="form.ip" style="width: 370px" />
          </el-form-item>
          <el-form-item label="通讯端口" prop="port">
            <el-input v-model="form.port" style="width: 370px" />
          </el-form-item>
          <el-form-item label="通道号" prop="channel">
            <el-input v-model="form.channel" style="width: 370px" />
          </el-form-item>
          <el-form-item label="帐号" prop="user">
            <el-input v-model="form.user" style="width: 370px" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" style="width: 370px" />
          </el-form-item>
          <el-form-item label="楼层" prop="floor">
            <el-select
              v-model="form.floor"
              style="width: 370px"
              placeholder="请选择楼层"
            >
              <el-option label="一楼" :value="1" />
              <el-option label="二楼" :value="2" />
              <el-option label="三楼" :value="3" />
              <el-option label="四楼" :value="4" />
              <el-option label="五楼" :value="5" />
              <el-option label="六楼" :value="6" />
            </el-select>
          </el-form-item>
          <el-form-item label="位置" prop="position">
            <el-input v-model="form.position" style="width: 370px" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="labName" label="实验室">
          <template slot-scope="scope">
            <span v-if="scope.row.labId">{{ scope.row.labName }}</span>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="IP地址" />
        <el-table-column prop="port" label="通讯端口" />
        <el-table-column prop="channel" label="通道号" />
        <el-table-column prop="floor" label="楼层">
          <template slot-scope="scope">
            <span v-if="scope.row.floor == 1">一楼</span>
            <span v-if="scope.row.floor == 2">二楼</span>
            <span v-if="scope.row.floor == 3">三楼</span>
            <span v-if="scope.row.floor == 4">四楼</span>
            <span v-if="scope.row.floor == 5">五楼</span>
            <span v-if="scope.row.floor == 6">六楼</span>
          </template>
        </el-table-column>
        <el-table-column prop="position" label="位置" />
        <el-table-column label="信息记录" width="150px" align="center">
          <template slot-scope="scope">
            <el-button
              @click.native="viedoList(scope.row)"
              type="text"
              size="small"
              >视频记录</el-button
            >
            <el-button
              @click.native="pictureList(scope.row)"
              type="text"
              size="small"
              >图像记录</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin', 'vidicon:edit', 'vidicon:del'])"
          label="操作"
          width="180px"
          align="center"
        >
          <template slot-scope="scope">
            <div style="display: flex">
              <el-button
                style="margin-right: 3px"
                size="mini"
                icon="el-icon-video-camera"
                type="info"
                @click="clickLogin(scope.row)"
              />
              <udOperation :data="scope.row" :permission="permission" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      class="dialog1"
      :close-on-click-modal="false"
      :visible.sync="view"
      width="65%"
      :title="vidiconName"
    >
      <iframe
        ref="iframeID"
        :src="videoUrl"
        noresize="noresize"
        style="width: 100%; height: 90%; overflow: hidden"
        frameborder="0"
      ></iframe>
      <div style="margin-top: 15px">
        <el-button
          @click.native="
            $router.push({ name: 'VidiconRecord', params: { name: rowName } })
          "
          type="info"
          plain
          >视频记录
        </el-button>
        <el-button
          type="info"
          plain
          @click.native="
            $router.push({ name: 'pictureRecord', params: { name: rowName } })
          "
          >拍照记录
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudVidicon from "@/api/vidicon";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get } from "@/api/lab";
import { getToken } from "@/utils/auth";
const defaultForm = {
  id: null,
  name: null,
  labId: null,
  ip: null,
  port: null,
  channel: null,
  user: null,
  password: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  floor: null,
  position: null,
};
export default {
  name: "Vidicon",
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "摄像机",
      url: "api/vidicon",
      idField: "id",
      sort: "id,desc",
      crudMethod: {
        ...crudVidicon,
      },
    });
  },
  data() {
    return {
      view: false,
      vidiconName: "",
      permission: {
        add: ["admin", "vidicon:add"],
        edit: ["admin", "vidicon:edit"],
        del: ["admin", "vidicon:del"],
      },
      rules: {
        name: [
          {
            required: true,
            message: "名称不能为空",
            trigger: "blur",
          },
        ],
        labId: [
          {
            required: true,
            message: "实验室不能为空",
            trigger: "change",
          },
        ],
        ip: [
          {
            required: true,
            message: "IP地址不能为空",
            trigger: "blur",
          },
        ],
        port: [
          {
            required: true,
            message: "通讯端口不能为空",
            trigger: "blur",
          },
        ],
        channel: [
          {
            required: true,
            message: "通道号不能为空",
            trigger: "blur",
          },
        ],
        user: [
          {
            required: true,
            message: "帐号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
        floor: [
          {
            required: true,
            message: "楼层不能为空",
            trigger: "blur",
          },
        ],
      },
      queryTypeOptions: [
        {
          key: "name",
          display_name: "名称",
        },
        {
          key: "labName",
          display_name: "实验室",
        },
        {
          key: "ip",
          display_name: "IP地址",
        },
      ],
      password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
      searchLoading: false,
      labs: [],
      searchForm: {
        page: 1,
        size: 10,
        name: "",
        id: "",
      },
      rowName: "",
      title: "开始录像",
      ip: "",
      port: "",
      user: "",
      password: "",
      labId: "",
      name: "",
      labName: "",
      token: "",
      vidiconId: "",
      videoUrl: "",
    };
  },
  mounted() {
    this.token = getToken();
  },
  destroyed() {
    window.removeEventListener("message", this.sendMessage);
  },
  created() {
    this.getLabs();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    getLabs(query = "") {
      this.searchForm.name = query;
      this.searchLoading = true;
      let newLabs = {
        id: 0,
        name: "无",
      };
      get()
        .then((response) => {
          this.searchLoading = false;
          this.labs = response;
          this.labs.unshift(newLabs);
          console.log(this.labs)
        })
        .catch((_) => {
          this.searchLoading = false;
        });
    },
    clickLogin(row) {
      // this.view = true;
      this.rowName = row.name;
      this.vidiconName = row.labName + "--" + row.name + "--" + row.ip;
      this.ip = row.ip;
      this.port = row.port;
      this.user = row.user;
      this.password = row.password;
      this.name = row.name;
      this.labId = row.labId;
      this.labName = row.labName;
      this.vidiconId = row.id;

      this.videoUrl =
          process.env.VUE_APP_CAMERA_URL+'/cn/video.html?ip=' +
          this.ip +
          '&port=' +
          this.port +
          '&user=' +
          this.user +
          '&password=' +
          this.password +
          '&token=' +
          this.token +
          '&labId=' +
          this.labId +
          '&vidiconId=' +
          this.vidiconId;

      window.open(this.videoUrl)

    },
    viedoList(row) {
      this.$router.push({ name: "VidiconRecord", params: { name: row.name } });
    },
    pictureList(row) {
      this.$router.push({ name: "pictureRecord", params: { name: row.name } });
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
  height: 100%;
}

.app-container ::v-deep.dialog1 .el-dialog {
  height: 80%;
}

.app-container ::v-deep.dialog1 .el-dialog__header {
  height: 30px;
}

.app-container ::v-deep.dialog1 .el-dialog__body {
  height: 100%;
}

/* .app-container /deep/.dialog1 .el-dialog__body {
    height: 100%;
  }  */
</style>
